﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PersonalFinanceManager.Repositories.Repositories;
using System.Drawing;
using System.Linq.Expressions;

namespace PersonalFinanceManager.Website
{
    public partial class ManageUsers : System.Web.UI.Page
    {
        UsersRepository usersRepo = new UsersRepository();
        bool DoSearch = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnUpdate.Visible = false;
            }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            IList<DataModel.Users> users;

            if (DoSearch)
            {
                string s = txtSearch.Text;
                Expression<Func<DataModel.Users, bool>> whereCondition = (x => x.FirstName.Contains(s)
                                                                            || x.Surname.Contains(s)
                                                                            || x.Username.Contains(s)
                                                                            || x.Email.Contains(s));
                users = usersRepo.FindAll(whereCondition, (x => x.RegistrationDate), Repositories.SortingMode.descending);
            }
            else
                users = usersRepo.GetAll(x => x.RegistrationDate, Repositories.SortingMode.descending);

            gvUsers.DataSource = users;
            gvUsers.DataBind();

            lblTotalCount.Text = users.Count.ToString();
        }

        protected void gvUsers_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id = Convert.ToInt32(e.Keys["ID"]);
            DataModel.Users user = new DataModel.Users();
            user.ID = id;

            usersRepo.Delete(user);
        }

        protected void gvUsers_SelectedIndexChanged(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(gvUsers.DataKeys[gvUsers.SelectedIndex]["ID"]);
            DataModel.Users user = usersRepo.GetSingle(x => x.ID == id);
            ViewState["ID"] = user.ID;

            txtFirstName.Text = user.FirstName;
            txtSurname.Text = user.Surname;
            txtUsername.Text =user.Username;
            txtPassword.Text = user.Password;
            txtEmail.Text =user.Email;

            ResetForm(false, user.Username);
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            DataModel.Users user = new DataModel.Users();
            user.FirstName = txtFirstName.Text;
            user.Surname = txtSurname.Text;
            user.Username = txtUsername.Text;
            user.Password = txtPassword.Text;
            user.Email = txtEmail.Text;
            user.isAdmin = false;
            user.Active = true;
            user.RegistrationDate = DateTime.Now;

            usersRepo.Insert(user);

            ResetForm(true, "");
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(ViewState["ID"]);
            DataModel.Users user = usersRepo.GetSingle(x => x.ID == id);
            user.FirstName = txtFirstName.Text;
            user.Surname = txtSurname.Text;
            user.Username = txtUsername.Text;
            user.Password = txtPassword.Text;
            user.Email = txtEmail.Text;

            usersRepo.Update(user);

            ResetForm(true, "");
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ResetForm(true, "");
        }

        protected void ResetForm(bool isInsert, string username)
        {
            if (isInsert)
            {
                btnInsert.Visible = true;
                btnUpdate.Visible = false;
                txtUsername.ReadOnly = false;
                txtUsername.BackColor = Color.White;
                pnlUsers.GroupingText = "Внеси корисник";
                pnlUsers.DefaultButton = "btnInsert";

                foreach (Control c in pnlUsers.Controls)
                    if (c is TextBox)
                        (c as TextBox).Text = "";
            }
            else
            {
                btnInsert.Visible = false;
                btnUpdate.Visible = true;
                txtUsername.ReadOnly = true;
                txtUsername.BackColor = Color.Gray;
                pnlUsers.GroupingText = "Промени корисник " + username;
                pnlUsers.DefaultButton = "btnUpdate";
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            DoSearch = true;
        }
    }
}